﻿//-----------------------------------------------------------------------
// <copyright file="WorkItemFormViewer.cs" company="Microsoft Corporation">
//     Copyright (c) Microsoft Corporation 2011. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Internal.TeamFoundationServer.Submit
{
    using System.Windows.Forms;
    using System.Windows.Forms.Integration;

    using Microsoft.TeamFoundation.WorkItemTracking.WpfControls;

    using Properties;

    /// <summary>
    /// BugFormViewer class.
    /// </summary>
    internal partial class WorkItemFormViewer : Form
    {
        /// <summary>
        /// Valid Work Item constant string.
        /// Used to tell the caller the work item state.
        /// </summary>
        public const string ValidWorkItem = "VALIDWORKITEM";

        /// <summary>
        /// WorkItemFormControl object.
        /// </summary>
        private readonly WorkItemControl workItemFormControl;

        /// <summary>
        /// The ctrl host.
        /// </summary>
        private readonly ElementHost ctrlHost = new ElementHost();

        /// <summary>
        /// Initializes a new instance of the <see cref="WorkItemFormViewer"/> class.
        /// </summary>
        public WorkItemFormViewer()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WorkItemFormViewer"/> class.
        /// </summary>
        /// <param name="workItemFormControl">WorkItemFormControl object.</param>
        public WorkItemFormViewer(WorkItemControl workItemFormControl)
            : this()
        {
            this.ctrlHost.Dock = DockStyle.Fill;
            this.panelControl.Controls.Add(this.ctrlHost);
            
            this.workItemFormControl = workItemFormControl;
            this.ctrlHost.Child = this.workItemFormControl;
        }

        /// <summary>
        /// Button next event.
        /// </summary>
        /// <param name="sender">Object sender.</param>
        /// <param name="e">Event arguments.</param>
        private void BtnNextClick(object sender, System.EventArgs e)
        {
            if (this.workItemFormControl.Item.Validate().Count > 0)
            {
                MessageBox.Show(
                    Resources.WorkItemFixFieldWarningString,
                    Resources.ErrorString,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error,
                    MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign & MessageBoxOptions.RtlReading);

                return;
            }

            this.Tag = ValidWorkItem;
            this.Close();
        }

        /// <summary>
        /// Button back event.
        /// </summary>
        /// <param name="sender">Object sender.</param>
        /// <param name="e">Event arguments.</param>
        private void BtnBackClick(object sender, System.EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Button cancel event.
        /// </summary>
        /// <param name="sender">Object sender.</param>
        /// <param name="e">Event arguments.</param>
        private void BtnCancelClick(object sender, System.EventArgs e)
        {
            this.Dispose();
        }

        /// <summary>
        /// Form Load event.
        /// </summary>
        /// <param name="sender">Object sender.</param>
        /// <param name="e">Event arguments.</param>
        private void WorkItemFormViewerLoad(object sender, System.EventArgs e)
        {
            this.Text = this.workItemFormControl.Item.Id + Resources.DashSeparator + this.workItemFormControl.Item.Title;
        }
    }
}
